/*
 * code.c
 *
 *  Created on: Nov 11, 2021
 *      Author: Abyss
 */

#include "main.h"
#include "oled.h"
#include "crcLib.h"
#include "sdcard.h"
#include "bmp280.h"
#include "sys.h"

extern ADC_HandleTypeDef hadc1;
extern UART_HandleTypeDef huart6;
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim10;
extern TIM_HandleTypeDef htim11;

#define ADC_MAX_NUM  4

float ADC_Values[ADC_MAX_NUM]={0} ;
int accelerator=0;  //油门
uint8_t RxBuffer;
uint8_t RxData[8];

void run()
{

	OLED_Init();
	OLED_Clear();
	HAL_TIM_Base_Start_IT(&htim10);
	OLED_Progress(60);

//	char showdata[20];
//	uint8_t da;
//	da = BMP280_ReadID();
//	sprintf(showdata,"0x%0X",da);
//	OLED_ShowString(16, 16, showdata, 12, 1);
//	Bmp_Init();
//    while(1)
//    {
//    	while(BMP280_GetStatus(BMP280_MEASURING) != RESET);
//		while(BMP280_GetStatus(BMP280_IM_UPDATE) != RESET);
//		sprintf(showdata,"\r\nPressure %ld",BMP280_Get_Pressure());
//		OLED_ShowString(64, 16, showdata, 12, 1);
//		HAL_Delay(200);
//    }

//	SD_Init();
//	char sd_cid[16]="null";
//	SD_GetCID(sd_cid);
//	printf("%s\r\n",sd_cid);
//
	OLED_Progress(100);

	OLED_Clear();
    Fly_Mode = Manual; //手动模式
	HAL_Delay(10);
	HAL_UART_Receive_IT(&huart6,&RxBuffer, 1);//该函数会开启接收中断：标志位UART_IT_RXNE，并且设置接收缓冲以及接收缓冲接收最大数据量
	HAL_TIM_Base_Start_IT(&htim11);
	HAL_TIM_Base_Start_IT(&htim1);
	while(1)
	{
		HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON,PWR_SLEEPENTRY_WFI);	 // 进入睡眠模式
	}
}




//定时器回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	UNUSED(htim);

	//刷新屏幕以及led闪烁
	if(htim == &htim10)
	{
		static int  time = 0,re=0;
		if(time++==60)
		{
			time = 0;
			HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
		}
		if(re++==1)
		{
	        OLED_Refresh();//60fps屏幕刷新
	        re=0;
		}

	}

	//中断1ms
	if(htim == &htim1)
	{
		static int time = 0;

		if(time++==10)
		{
			time=0;
	        static float x1,y1,x2,y2;
	        x1 = ((ADC_Values[0]/16)<0)?0:ADC_Values[0]/16;  //传输数据校正
	        y1 = (256-ADC_Values[1]/16<0)?0:256-ADC_Values[1]/16;
	        x2 = (ADC_Values[2]/16<0)?0:ADC_Values[2]/16;
	        y2 = (256-ADC_Values[3]/16<0)?0:ADC_Values[3]/16;

	 	    char show_data1[10], show_data2[10], show_data3[10], show_data4[10];
	        sprintf(show_data1,"%d      ",(int)x1);
	        sprintf(show_data2,"%d      ",(int)y1);
	        sprintf(show_data3,"%d      ",(int)x2);
	        sprintf(show_data4,"%d      ",(int)y2);
	        OLED_ShowString(16, 0, show_data1, 12, 1);
	        OLED_ShowString(16, 16, show_data2, 12, 1);
	        OLED_ShowString(16, 32, show_data3, 12, 1);
	        OLED_ShowString(16, 48, show_data4, 12, 1);

//
	        SendTo_plane(Fly_Mode,(int)x1,(int)y1,(int)x2,(int)y2);
		}
	}

	//中断采集adc
	if(htim == &htim11)
	{
		static int AD[4][10],x=0;

	   for(int i=0;i<4;i++)
	   {
		   HAL_ADC_Start(&hadc1);

		   HAL_ADC_PollForConversion(&hadc1,0xffff);//等待ADC转换完成

		   AD[i][x] = HAL_ADC_GetValue(&hadc1);

	   }
	   if(++x==10)x=0;

	   for(int y=0;y<4;y++)
	   {
		   for(int z=0;z<10;z++)
		   {
			   ADC_Values[y] += AD[y][z];
		   }
		   ADC_Values[y] /= 10;
	   }
	}
}

//按键中断
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  UNUSED(GPIO_Pin);
  int da=0;
  switch(GPIO_Pin)
   {
     case GPIO_PIN_14:  break;
     case GPIO_PIN_15:  break;
     default: break;
   }
}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
  UNUSED(hadc);
  if(hadc == &hadc1)
  {

  }
}





